/*
 * ThreadKeepCharged.cpp
 *
 *  Created on: Jan 27, 2013
 *      Author: Drew Prevost
 */

#include "ThreadKeepCharged.h"

ThreadKeepCharged::ThreadKeepCharged(
                                      uint8_t nVmPin,
                                      int nVmPinMin,
                                      int nVmPinMax,
                                      int nVmVoltsMin,
                                      int nVmVoltsMax,
                                      unsigned long nVmReadFreqMicros,
                                      uint8_t nEngServoPin,
                                      unsigned long nEngServoMaxSpeedInMicrosPerDegree,
                                      unsigned int nEngServoInitialPosition,
                                      uint8_t nEngPinGlowPlug,
                                      uint8_t nEngPinGenConnect,
                                      uint8_t nEngPinStarterMotor,
                                      uint8_t nEngPinGenDisonnect,
                                      uint8_t nEngPinGenMonitor )
    : threadVoltageMonitor( nVmPin, nVmPinMin, nVmPinMax, nVmVoltsMin, nVmVoltsMax, nVmReadFreqMicros ),
      threadServoEngineExhaust( nEngServoPin, nEngServoMaxSpeedInMicrosPerDegree, nEngServoInitialPosition, 0 ),
      threadEngineStart( threadServoEngineExhaust, nEngPinGlowPlug, nEngPinGenConnect, nEngPinStarterMotor, nEngPinGenDisonnect, nEngPinGenMonitor ),
      threadEngineStop( threadServoEngineExhaust, nEngPinGlowPlug, nEngPinGenConnect, nEngPinStarterMotor, nEngPinGenDisonnect, nEngPinGenMonitor )
{

}

ThreadKeepCharged::~ThreadKeepCharged()
{
}

bool ThreadKeepCharged::run()
{
  threadVoltageMonitor.run();
  threadServoEngineExhaust.run();
  threadEngineStart.run();
  threadEngineStop.run();

  PT_BEGIN()
  ;

  PT_SPAWN( threadVoltageMonitor );

  if ( threadVoltageMonitor.isVoltageTooLow() )
  {
    PT_SPAWN( threadEngineStart );
  }
  else if ( threadVoltageMonitor.isVoltageTooHigh() )
  {
    PT_SPAWN( threadEngineStop );
  }

PT_END()
;
return false;
}
